#include "gameplay.h"
#include "qdatetime.h"
#include "qevent.h"
#include<QTimer>
#include<QKeyEvent>


gamePlay::gamePlay( QWidget *parent) : QWidget(parent)

{

    stop.setText("退出");

    clife.setRange(0,1000);
    clife.setValue(1000);

    clife.move(100,0);
    clife.setFixedSize(160,20);

    gameview.setSceneRect(QRect(0,0,500,280));
    gamescene.setSceneRect(QRect(0,0,500,280));

    background1.setPixmap(QPixmap("bgd.png"));
    background2.setPixmap(QPixmap("bgd.png"));

    tom.setPixmap(QPixmap("tomfly.png"));
    jerry.setPixmap(QPixmap("jerryfly.png"));

    tree.setPixmap(QPixmap("tree.png"));
    yestar.setPixmap(QPixmap("yestar.png"));
    redstar.setPixmap(QPixmap("redstar.png"));

    gamescene.addItem(&  background1);
    gamescene.addItem(&  background2);
    gamescene.addItem(&  tree);
    gamescene.addItem(&  yestar);
    gamescene.addItem(&  redstar);
    gamescene.addItem(&  temp);
    gamescene.addWidget(&  clife);
    gamescene.addWidget(&  stop);
    gamescene.addWidget(&  sscore);

    //树
    srand(QTime(0,0,0).secsTo(QTime::currentTime()));
    num=rand()%24;
    xn=rand()%24,yn=rand()%12;
    yestar.setPos(500+xn*20,(yn+1)*20);
    xr=rand()%24,yr=rand()%3;
    redstar.setPos(500+xr*20,(yr+1)*30);
    tree.setPos(500+num*20,144);

    mtree.start(10);
    connect(&mtree,&QTimer::timeout,this,&gamePlay::treemove);

    touchtree.start(10);
    connect(&touchtree,&QTimer::timeout,this,&gamePlay::touchingtree);

    myestar.start(10);
    connect(&myestar,&QTimer::timeout,this,&gamePlay::yestarmove);

    mredstar.start(10);
    connect(&mredstar,&QTimer::timeout,this,&gamePlay::redstarmove);

    sscore.setParent(this);
    sscore.setText("得分：0");
    sscore.setFixedSize(100,30);
    sscore.move(400,0);
    sscore.setStyleSheet("background-color: transparent; border: none;font-family: 楷体; font-size: 16px; color: #000000;");

    cscore.start(10);
    connect(&cscore,&QTimer::timeout,this,&gamePlay::increasescore);

    // stop.move(0,0);
    stop.setFixedSize(50,25);
    connect(&stop,&QAbstractButton::clicked,this,&gamePlay::message);
    stop.setStyleSheet("background-color: transparent; border: none;font-family: 华文彩云; font-size: 24px; color: #34BCF2;");

    dlife.start(100);
    connect(&dlife,&QTimer::timeout,this,&gamePlay::decrease);


    temp.setPixmap(QPixmap("tomfly"));
    background2.setPos(500,0);

    gameview.setParent(this);
    gameview.setScene(&gamescene);


    bgm.start(10);
    connect(&bgm,&QTimer::timeout,this,&gamePlay::bgmove);

    cmove.start(10);
    connect(&cmove,&QTimer::timeout,this,&gamePlay::charactermove);

    connect(&speedTimer,&QTimer::timeout, this, &gamePlay::increaseSpeed);
    speedTimer.start(20000); // 每 20 秒增加一次速度

    cover.start(10);
    connect(&cover,&QTimer::timeout,this,&gamePlay::gameover);


}

void gamePlay::charactermove()
{
    for(int keyc:mykeylist)
    {
        switch(keyc)
        {
        case Qt::Key_W:temp.moveBy(0,-5);break;
        case Qt::Key_A:temp.moveBy(-5,0);break;
        case Qt::Key_S:temp.moveBy(0,5);break;
        case Qt::Key_D:temp.moveBy(5,0);break;
        }
    }


    if(temp.x()<0){
        temp.setX(0);
    }
    if(temp.x()>500-temp.pixmap().width()){
        temp.setX(500-temp.pixmap().width());
    }
    if(temp.y()<0){
        temp.setY(0);
    }
    if(temp.y()>280-temp.pixmap().height()){
        temp.setY(280-temp.pixmap().height());
    }
}

void gamePlay::keyPressEvent(QKeyEvent *event)
{
    //添加按键组合
    switch(event->key())
    {
    case Qt::Key_W:
    case Qt::Key_A:
    case Qt::Key_S:
    case Qt::Key_D:
    mykeylist.append(event->key());
    break;
    }
    if(event->key()==Qt::Key_Z){
        if(score>=500*speed){
        temp.moveBy(0,100);
            score-=500*speed;
        }
    }
    if(event->key()==Qt::Key_Q){
        if(score>=500*speed){
            temp.moveBy(0,-100);
            score-=500*speed;
        }
    }
};

void gamePlay::keyReleaseEvent(QKeyEvent *event)
{
    //移除对应按键组合
    if(mykeylist.contains(event->key()))
    {
        mykeylist.removeOne(event->key());
    }

};

